#include <Windows.h>
#include "CHud.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "CPlayer.h"
#include "CSpellBase.h"

void CHud::SetXPosition(string fileName, int x)
{
	hudObjects[fileName].posX = x;
}

void CHud::SetYPosition(string fileName, int y)
{
	hudObjects[fileName].posY = y;
}

void CHud::SetXScale(string fileName, float fScale)
{
	hudObjects[fileName].Xscale = fScale;
}

void CHud::SetYScale(string fileName, float fScale)
{
	hudObjects[fileName].Yscale = fScale;
}

void CHud::SetRotation(string fileName, float fRotation)
{
	hudObjects[fileName].rotation = fRotation;
}

///////////////////////////////////////////////////
//	Function	:	"LoadHUDObjects"
//	
//	Last Modified:	January 21, 2012 (SMS)
//
//	Input		:	none
//										
//	Return		:	none
//
//	Purpose		:	Loads the border, Xbox360 buttons, player icons, that will be used in the HUD bar
//////////////////////////////////////////////////
void CHud::LoadHUDObjects()
{
	CreateHudObject("A button.png",  83, 568, 32, 32, 1.0f, 1.0f, 0, D3DCOLOR_XRGB(255, 255, 255));
	CreateHudObject("B button.png", 183, 568, 32, 32, 1.0f, 1.0f, 0, D3DCOLOR_XRGB(255, 255, 255));
	CreateHudObject("X button.png", 283, 568, 32, 32, 1.0f, 1.0f, 0, D3DCOLOR_XRGB(255, 255, 255));
	CreateHudObject("Y button.png", 383, 568, 32, 32, 1.0f, 1.0f, 0, D3DCOLOR_XRGB(255, 255, 255));
	CreateHudObject("hud bar border.png", 0, 499, 800, 100, 1.0f, 1.0f, 0, D3DCOLOR_XRGB(255, 255, 255));
	// TODO: create the player icons
}

void CHud::CreateHudObject(string fileName, int nPosX, int nPosY, int nWidth, int nHeight, float fScaleX, float fScaleY, float fRotation, DWORD dwColor)
{
	//const char* filename = fileName.c_str();

	hudObjects[fileName].name = fileName.c_str();
	hudObjects[fileName].posX = nPosX;
	hudObjects[fileName].posY = nPosY;
	hudObjects[fileName].width = nWidth;
	hudObjects[fileName].height = nHeight;
	hudObjects[fileName].Xscale = fScaleX;
	hudObjects[fileName].Yscale = fScaleY;
	hudObjects[fileName].rotation = fRotation;
	hudObjects[fileName].color = dwColor;

	hudObjects[fileName].centerX = hudObjects[fileName].width >> 1;
	hudObjects[fileName].centerY = hudObjects[fileName].height >> 1;

	string texturePath = "resources/graphics/";
	texturePath += fileName;

	hudObjects[fileName].imageID = CSGD_TextureManager::GetInstance()->LoadTexture(texturePath.c_str(), dwColor);
}
	
///////////////////////////////////////////////////
//	Function	:	"DrawHUDbar"
//	
//	Last Modified:	January 21, 2012 (SMS)
//
//	Input		:	none
//										
//	Return		:	none
//
//	Purpose		:	draws the HUD border bar, the Xbox 360 buttons, and the player icons
//////////////////////////////////////////////////
void CHud::DrawHUDBar(CPlayer* playerOne, CPlayer* playerTwo)
{
	Draw("hud bar border.png");

	// player one Xbox 360 buttons
	SetXPosition("A button.png",  83);
	SetXPosition("B button.png", 147);
	SetXPosition("X button.png", 211);
	SetXPosition("Y button.png", 275);

	SetYPosition("A button.png", 568);
	SetYPosition("B button.png", 568);
	SetYPosition("X button.png", 568);
	SetYPosition("Y button.png", 568);

	Draw("A button.png");
	Draw("B button.png");
	Draw("X button.png");
	Draw("Y button.png");

	// player two Xbox 360 buttons
	SetXPosition("Y button.png", 686);
	SetXPosition("X button.png", 622);
	SetXPosition("B button.png", 558);
	SetXPosition("A button.png", 494);

	SetYPosition("A button.png", 568);
	SetYPosition("B button.png", 568);
	SetYPosition("X button.png", 568);
	SetYPosition("Y button.png", 568);

	Draw("A button.png");
	Draw("B button.png");
	Draw("X button.png");
	Draw("Y button.png");

	// draw the spell icons
	/*if (NULL != playerOne)
		DrawSpellIcons(playerOne);*/

	// if there is a second player, draw his icons
	/*if (NULL != playerTwo)
		DrawSpellIcons(playerTwo);*/
}

void CHud::Draw(string fileName)
{
	const char* filename = fileName.c_str();
	CSGD_TextureManager::GetInstance()->Draw(hudObjects[fileName].imageID, hudObjects[fileName].posX, hudObjects[fileName].posY,
		hudObjects[fileName].Xscale, hudObjects[fileName].Yscale, 0, hudObjects[fileName].centerX, hudObjects[fileName].centerY, 
		hudObjects[fileName].rotation, hudObjects[fileName].color);
}

///////////////////////////////////////////////////
//	Function	:	"DrawSpellIcons"
//	
//	Last Modified:	January 21, 2012 (SMS)
//
//	Input		:	player	-	a pointer to the player who's icons you want to display
//										
//	Return		:	none
//
//	Purpose		:	To draw player 1's or player 2's spell icons to the HUD bar
//////////////////////////////////////////////////
void CHud::DrawSpellIcons(CPlayer* player)
{
	int xPosPlayerOne = 83;
	int xPosPlayerTwo = 494;
	int yPos = 536; 

	// TODO: Assign image icon IDs to the spells when you add them to a player's spell slots
	switch(player->GetPlayerNumber() )
	{
	case 1:
		{
			// go through the four spells the player may have
			for (int index = 0; index < 4; index++)
			{
				// for each slot that has been assigned a spell already
				if (player->GetSpellSlot(index) == NULL)
				{
					// render that spell's icon image
					CSGD_TextureManager::GetInstance()->Draw(player->GetSpellSlot(index)->GetIconID(), xPosPlayerOne, yPos);

					xPosPlayerOne += 64;
				}
			}
			
		}
		break;

	case 2:
		{
			// go through the four spells the player may have
			for (int index = 0; index < 4; index++)
			{
				// for each slot that has been assigned a spell already
				if (player->GetSpellSlot(index) == NULL)
				{
					// render that spell's icon image
					CSGD_TextureManager::GetInstance()->Draw(player->GetSpellSlot(index)->GetIconID(), xPosPlayerTwo, yPos);

					xPosPlayerTwo += 64;
				}
			}
		}
		break;
	}
}

//CHud::CHud()
//{
//	
//}
//
//CHud::~CHud()
//{
//	
//}
//
//void CHud::Update()
//{
//
//}
//
//void CHud::Render()
//{
//	/*
//	char szBuffer[256];
//		printf_s(szBuffer,"resource/graphics/%c",GetFileName());*/
//	
//	CreateHudObject(GetFileName(),GetHieght(),GetWidth(),GetPosX(),GetPosY(),GetImageID());
//}
//
//void CHud::CreateHudObject(  string szFileName, int nHieght, int nWidth, int PosX, int PosY, int nImageID)
//{
//	SetFileName(szFileName);
//	SetPosX(PosX);
//	SetPosY(PosY);
//	SetHieght(nHieght);
//	SetWidth(nWidth);
//	RECT rSize = {GetWidth(),GetHieght(),0,0};
//	nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(GetFileName().c_str());
//	CSGD_TextureManager::GetInstance()->Draw(GetImageID(),GetPosX(),GetPosY(),1,1,&rSize);
//}
